﻿using System.IO;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using Bookvar.ObjectModel;
using Bookvar.UI.ViewModel;

namespace BookvarWpf.Windows
{
    /// <summary>
    /// Interaction logic for SampleWindow.xaml
    /// </summary>
    public partial class AddNotesWindow
    {
        private readonly NotesWindowViewModel viewModel;

        public AddNotesWindow( Topic topic )
        {
            InitializeComponent();

            viewModel = new NotesWindowViewModel( topic );
            ViewModelBase.AddCommandAndInputBindings( this, viewModel );

            if ( topic.Note != null )
            {
                using ( var stream = new MemoryStream() )
                {
                    topic.Note.Save( stream, DataFormats.XamlPackage );
                    TextRange topicNotes = new TextRange( notes.Document.ContentStart, notes.Document.ContentEnd );
                    topicNotes.Load( stream, DataFormats.XamlPackage );
                    notes.CaretPosition = notes.Document.ContentEnd;
                }
            }

            Loaded += delegate { notes.Focus(); };
        }

        private void OnOkClick( object sender, RoutedEventArgs e )
        {
            ( (ButtonBase) sender ).CommandParameter =
                new TextRange( notes.Document.ContentStart, notes.Document.ContentEnd );
            CloseCommand.Execute( null, this );
        }
    }
}